/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/


#include "actionwidget.h"
#include "ui_actionwidget.h"

#include "other/gameStructs.h"

#include <QMessageBox>

ActionWidget::ActionWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ActionWidget)
{
    ui->setupUi(this);
}

ActionWidget::~ActionWidget()
{
    delete ui;
}

void ActionWidget::clearActionForm() {
    ui->actionAddItemKeys->setText("");
    ui->actionAddUnitKey->setText("");
    ui->actionReplaceModel->setChecked(false);
    ui->actionAmountColumn->setCurrentIndex(0);
    ui->actionAmountValue->setText("");
    ui->actionAmountRadio->setChecked(true);
    ui->actionAllowedItemKeys->setText("");
    ui->actionGlobalTextKeys->setText("");
    // \NOTE: ItemLimits should be cleared here
    ui->actionUnlockedOptionKeys->setText("");
    ui->actionShowOptionKeys->setText("");
    ui->actionScript->setPlainText("");
}

Actions ActionWidget::getActions() const {
    return internalActions;
}

void ActionWidget::setActions(const Actions& actions) {
    ui->actionList->clear();
    internalActions = actions;
    for(int i = 0; i<internalActions.size(); ++i) {
        ui->actionList->addItem(ui->actionType->itemText(internalActions.at(i)->type()));
    }
}


void ActionWidget::clearAll() {
    clearActionForm();
}

void ActionWidget::changeEvent(QEvent *event) {
    if(event->type() == QEvent::LanguageChange)
        ui->retranslateUi(this);
    QWidget::changeEvent(event);
}

void ActionWidget::on_actionSetItemLimits_clicked()
{
    // \TODO: Implement setItemLimits_clicked()
}

void ActionWidget::on_actionList_currentRowChanged(int currentRow)
{
    clearActionForm();
    // \TODO: Process all option types
    if(currentRow > -1) {
        const QSharedPointer<ObjectAction> &action = internalActions.at(currentRow);
        ui->actionUpdate->setEnabled(true);
        ui->actionDelete->setEnabled(true);
        ObjectAction::ActionType type = action->type();
        ui->actionType->setCurrentIndex(type);
        switch(type) {
        case ObjectAction::AddItems: {
            AddItemAction *act = static_cast<AddItemAction*>(action.data());
            ui->actionAddItemKeys->setText(act->getItems().join(","));
            break;
        } case ObjectAction::AddUnit: {
            AddUnitAction *act = static_cast<AddUnitAction*>(action.data());
            ui->actionAddUnitKey->setText(act->getUnitKey());
            ui->actionReplaceModel->setChecked(act->replacesModel());
            break;
        }
        case ObjectAction::ModifyCell: {
            ModifyCellAction *act = static_cast<ModifyCellAction*>(action.data());
            ui->actionAmountColumn->setCurrentIndex(ui->actionAmountColumn->findData(act->getCellKey()));
            ui->actionAmountValue->setText(act->getValue());
            ui->actionAppendRadio->setChecked(act->getOnlyAppend());
            break;
        }
        case ObjectAction::ChangeAllowedItems: {
            //ui->actionAllowedItemKeys->setText(action.value.toStringList().join(","));
            break;
        }
        case ObjectAction::ChangeGlobalTexts: {
            //ui->actionGlobalTextKeys->setText(action.value.toStringList().join(","));
            break;
        }
        case ObjectAction::ChangeItemLimits: {

            break;
        }
        case ObjectAction::ChangeLockedOptions: {
            //ui->actionAllowedOptionKeys->setText(action.value.toStringList().join(","));
            break;
        }
        case ObjectAction::ChangeShownOptions: {
            //ui->actionShowOptionKeys->setText(action.value.toStringList().join(","));
            break;
        }
        case ObjectAction::CustomScript: {
            //ui->actionScript->setPlainText(action.value.toString());
            break;
        }
        default:
            QMessageBox::critical(this, tr("Unexpected value"), tr("The method on_actionList_currentRowChanged() got an unexpected value. Please report this in http://code.google.com/p/yalb"));
        }
    } else {
        ui->actionUpdate->setEnabled(false);
        ui->actionDelete->setEnabled(false);
    }
}

void ActionWidget::on_actionType_currentIndexChanged(int index)
{
    ui->actionStackedW->setCurrentIndex(index);
}

QSharedPointer<ObjectAction> ActionWidget::getActionFromUI() {
    switch(ui->actionType->currentIndex()) {
    case ObjectAction::AddItems:
        return QSharedPointer<ObjectAction>(new AddItemAction(ui->actionAddItemKeys->text().split(',')));
    case ObjectAction::AddUnit:
        return QSharedPointer<ObjectAction>(new AddUnitAction(ui->actionAddUnitKey->text(),
                                                              ui->actionReplaceModel->isChecked()));
    case ObjectAction::ModifyCell: {
        return QSharedPointer<ObjectAction>(new ModifyCellAction(
                                                ui->actionAmountColumn->itemData(ui->actionAmountColumn->currentIndex()).toString(),
                                                ui->actionAmountValue->text(), ui->actionAppendRadio->isChecked()));
        break;
    }
    case ObjectAction::ChangeAllowedItems: {
        //ui->actionAllowedItemKeys->setText(action.value.toStringList().join(","));
        break;
    }
    case ObjectAction::ChangeGlobalTexts: {
        //ui->actionGlobalTextKeys->setText(action.value.toStringList().join(","));
        break;
    }
    case ObjectAction::ChangeItemLimits: {

        break;
    }
    case ObjectAction::ChangeLockedOptions: {
        //ui->actionAllowedOptionKeys->setText(action.value.toStringList().join(","));
        break;
    }
    case ObjectAction::ChangeShownOptions: {
        //ui->actionShowOptionKeys->setText(action.value.toStringList().join(","));
        break;
    }
    case ObjectAction::CustomScript: {
        //ui->actionScript->setPlainText(action.value.toString());
        break;
    }
    default:
        QMessageBox::critical(this, tr("Unexpected value"), tr("The method getActionFromUI() got an unexpected value. Please report this in http://code.google.com/p/yalb"));
    }
    return QSharedPointer<ObjectAction>(NULL);
}

void ActionWidget::on_actionAdd_clicked()
{
    internalActions.append(getActionFromUI());
    ui->actionList->addItem(ui->actionType->currentText());
    clearActionForm();
}

void ActionWidget::on_actionUpdate_clicked()
{
    int row = ui->actionList->currentRow();
    internalActions[row] = getActionFromUI();
    ui->actionList->item(row)->setText(ui->actionType->currentText());
}

void ActionWidget::on_actionDelete_clicked()
{
    int row = ui->actionList->currentRow();
    delete ui->actionList->takeItem(row);
    internalActions.remove(row);
}
